<?php
namespace Admin\Model;
use Think\Model;
class CourseCategoryModel extends Model 
{
	protected $insertFields = array('name','create_time','company_id');
	protected $updateFields = array('id','name','create_time');
	protected $_validate = array(
		array('name', '1,50', '分类名的值最长不能超过 50 个字符！', 2, 'length',1),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($name = I('get.name'))
			$where['name'] = array('like', "%$name%");
		$create_timefrom = I('get.create_timefrom');
		$create_timeto = I('get.create_timeto');
		if($create_timefrom && $create_timeto)
			$where['create_time'] = array('between', array(strtotime("$create_timefrom 00:00:00"), strtotime("$create_timeto 23:59:59")));
		elseif($create_timefrom)
			$where['create_time'] = array('egt', strtotime("$create_timefrom 00:00:00"));
		elseif($create_timeto)
			$where['create_time'] = array('elt', strtotime("$create_timeto 23:59:59"));

        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = ['eq',session('admin.company_id')];
        }
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
        $data['create_time'] = date("Y-m-d H:i:s");
        $data['company_id'] = session('admin.company_id');
    }
	// 添加前
	protected function _before_update(&$data, $option)
	{
        $data['create_time'] = date("Y-m-d H:i:s");
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}